<?php

class std
{

    static function inc_class($class_name)
    {
        static $classes_declarations = array (
            "component" => "component",
            "cubic" => "cubic",
            "db"  => "db",
            "dom" => "dom",
            "fs" => "fs",
            "graphic" => "graphic",
            "mysql" => "mysql",
            "page" => "page",
            "pcre" => "pcre",
            "request" => "request",
            "rnd" => "rnd",
            "std" => "std",
            "str" => "str",
            "sys" => "sys",
            "vector" => "vector",
            "xml" => "xml",
            "xsl" => "xsl",
        );
        if (!class_exists($class_name)) {
            if (vector::key_exists($class_name, $classes_declarations) && fs::file_exists($fn = CLASSES_ROOT.$classes_declarations[$class_name].".".INCLUDE_EXTENSION)) {
                include_once($fn);
            } else {
                exit("Can`t include class: <strong>{$class_name}</strong>({$fn})");
            }
        }
    }

    static function &GetInstance($instance_name)
    {
        static $instances_declarations = array (
            "db" => array (
                "class" => "db",
                "arg"   => array (/* "\"localhost\"", "\"files\"", "\"\"", "\"files\"", "true"*/)
            ),
            "xml" => array (
                "class" => "xml",
                "arg"   => array ()
            ),
        );

        static $instances = array ();

        if (!vector::key_exists($instance_name, $instances)) {
            if (vector::key_exists($instance_name, $instances_declarations)) {
                std::inc_class($instances_declarations[$instance_name]["class"]);
            } else {
                exit("Can`t include instance: <strong>{$instance_name}</strong>");
            }
            $eval_arg = vector::implode(", ", $instances_declarations[$instance_name]["arg"]);
            eval("\$instances[\$instance_name] = new {$instances_declarations[$instance_name]["class"]}({$eval_arg});");
        }
        return $instances[$instance_name];
    }

    static function get_color_by_type($var)
    {
        $color = "#000000";
        switch (true) {
            case is_array($var): $color = "#FFFF00"; break;
            case is_bool($var): $color = "#800000"; break;
            case is_double($var): $color = "#0080FF"; break;
            case is_float($var): $color = "#0080FF"; break;
            case is_int($var): $color = "#FF0000"; break;
            case is_integer($var): $color = "#FF0000"; break;
            case is_long($var): $color = "#FF0000"; break;
            case is_null($var): $color = "#000000"; break;
            case is_string($var): $color = "#008000"; break;
        }

        return $color;
    }

    static function header($str)
    {
        return header($str);
    }

    static function filedump($filename)
    {
        std::header("Content-type: ".fs::mime($filename));
        if (file_exists($filename)) {
            echo fs::content($filename);
        } else {
            echo $filename;
        }
        exit;
    }

    static function vardump($var, $name = null, $first = true)
    {
        if (!is_null($name)) {
            echo "<div style=\"font-weight: bold; font-family: Consolas;\">{$name}:</div>";
        }
        if ($first) {
            echo "<hr/>";
        }
        echo "<div style=\"margin-left: 10px;\">";
        switch (true) {
            case is_array($var):
                foreach ( $var as $key => $value ) {
                    echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
                    echo "<tr>";
                    echo "<td style=\"vertical-align: top; color: ".std::get_color_by_type($key)."; font-weight: bold; font-family: Consolas;\">{$key}<span style=\"color: black;\">&nbsp;=>&nbsp;</span></td>";
                    echo "<td>";
                    std::vardump($value, null, false);
                    echo "</td>";
                    echo "</tr>";
                    echo "</table>";
                }
                break;
            case is_bool($var):
                echo "<span style=\"color: ".std::get_color_by_type($var)."; font-weight: bold; font-family: Consolas;\">".($var ? "true" : "false")."</div>";
                break;
            case is_double($var):
                echo "<span style=\"color: ".std::get_color_by_type($var)."; font-weight: bold; font-family: Consolas;\">{$var}</div>";
                break;
            case is_float($var):
                echo "<span style=\"color: ".std::get_color_by_type($var)."; font-weight: bold; font-family: Consolas;\">{$var}</div>";
                break;
            case is_int($var):
            case is_integer($var):
            case is_long($var):
                echo "<span style=\"color: ".std::get_color_by_type($var)."; font-weight: bold; font-family: Consolas;\">{$var}</div>";
                break;
            case is_null($var):
                echo "<span style=\"color: ".std::get_color_by_type($var)."; font-weight: bold; font-family: Consolas;\">NULL</div>";
                break;
            case is_string($var):
                echo "<span style=\"color: ".std::get_color_by_type($var)."; font-weight: bold; font-family: Consolas;\">".htmlspecialchars($var)."</div>";
                break;
        }
        echo "</div>";
    }

    static function time()
    {
        list($usec, $sec) = explode(" ", microtime());
        return sprintf("%.0f", ((double)$usec + (double)$sec)*1000);
    }
}

?>